package com.ccsc.auth.controller;

import com.ccsc.auth.entity.Department;
import com.ccsc.auth.entity.vi.DepartmentVi;
import com.ccsc.auth.entity.vo.DepartmentVo;
import com.ccsc.auth.service.DepartmentService;
import com.ccsc.auth.utils.BaseResponse;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * Created by yyangs on 2017/2/17.
 */
@RestController
@RequestMapping("/departmentManage")
public class DepartmentController extends BaseController {
    @Autowired
    private DepartmentService departmentService;

    //获取全部部门信息
    @RequestMapping(value = "/departments", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getAllDepartment(WebRequest request) throws Exception {
        try {
            List<DepartmentVo> depList = Lists.newArrayList();
            List<Department> departmentList = departmentService.getAllDepartments();
            if(departmentList!=null && departmentList.size()>0) {
                for (Department department : departmentList) {
                    DepartmentVo departmentVo = department2DepartmentVo(department);
                    depList.add(departmentVo);
                }
            }
            //添加root节点
            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setId(0);
            departmentVo.setName("组织机构");
            departmentVo.setpId(null);
            depList.add(departmentVo);
            return this.ok(depList);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //查询部门详细信息根据id
    @RequestMapping(value = "/department/{departmentIdIn}", method = RequestMethod.GET, produces = {"application/json", "application/xml"})
    public BaseResponse getDepartmentById(@PathVariable("departmentIdIn")String departmentIdIn) throws Exception {
        Integer departmentId=Integer.parseInt(departmentIdIn);
        try {
            Department department=departmentService.getDepartmentById(departmentId);
            DepartmentVo departmentVo = new DepartmentVo();
            BeanUtils.copyProperties(departmentVo, department);
            return this.ok(departmentVo);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //添加新部门
    @RequestMapping(value = "/department", method = RequestMethod.POST, produces = {"application/json", "application/xml"})
    public BaseResponse addDepartment(@Valid @RequestBody DepartmentVi departmentVi, BindingResult bindingResult) throws Exception {
        try {
            Integer result=0;
            Department department = departmentVi2Department(departmentVi);
            List<Department> departmentList=departmentService.getDepartmentsByName(department.getName());
            if(departmentList.size()>0) {
                return this.error(500, "部门名称已存在，添加失败");
            }
            result = departmentService.createDepartment(department);
            return this.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //修改部门
    @RequestMapping(value = "/department/{departmentIdIn}", method = RequestMethod.PUT, produces = {"application/json", "application/xml"})
    public BaseResponse updateDepartment(@PathVariable("departmentIdIn")String departmentIdIn, @Valid @RequestBody DepartmentVi departmentVi, BindingResult bindingResult) throws Exception {
        try {
            Integer result=0;
            departmentVi.setId(Integer.parseInt(departmentIdIn));
            Department departmentTemp=departmentService.getDepartmentById(departmentVi.getId());
            if(departmentTemp==null) {
                return this.error(500, "部门不存在，修改失败");
            }
            Department department = departmentVi2Department(departmentVi);
            List<Department> departmentList=departmentService.getDepartmentsByName(department.getName());
            if(departmentList.size()>0) {
                return this.error(500, "部门名称已存在，修改失败");
            }
            result = departmentService.updateDepartment(department);
            return this.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }

    //删除部门
    @RequestMapping(value = "/department/{departmentIdIn}", method = RequestMethod.DELETE, produces = {"application/json", "application/xml"})
    public BaseResponse deleteDepartment(@PathVariable("departmentIdIn")String departmentIdIn) throws Exception {
        Integer departmentId=Integer.parseInt(departmentIdIn);
        try {
            Integer result=0;
            Department department = departmentService.getDepartmentById(departmentId);
            if(department!=null) {
                result= departmentService.deleteDepartmentById(departmentId);
            }
            return this.ok(result);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(500, e.getMessage());
        }
    }
    public static DepartmentVo department2DepartmentVo(Department department){
        DepartmentVo departmentVo=new DepartmentVo();
        departmentVo.setId(department.getId());
        departmentVo.setName(department.getName());
        if(department.getParentDepartment()!=null) {
            departmentVo.setpId(department.getParentDepartment().getId());
        }
        return departmentVo;
    }

    public static Department departmentVi2Department(DepartmentVi departmentVi){
        Department department=new Department();
        department.setId(departmentVi.getId());
        department.setName(departmentVi.getName());
        if(departmentVi.getpId()!=0) {
            Department parentDepartment =new Department();
            parentDepartment.setId(departmentVi.getpId());
            department.setParentDepartment(parentDepartment);
        }else {
            department.setParentDepartment(null);
        }
        return department;
    }
}
